﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Collections;
using System.Text;


/// <summary>
/// Summary description for Str
/// </summary>
namespace Utils.Str
{
public class Str
		{

		public static void Sanitize(ref string s , string quoteChar ) 
		{
				s = quoteChar + s + quoteChar ;
		} //eof method Sinitize (ref string s , char quoteChar ) 

		public static string Sanitize ( object s , string quoteChar )
		{
				string str = s.ToString ().Trim();
				str = quoteChar + s + quoteChar;
				return str;
		} //eof method Sanitize ( object s , char quoteChar )


		public static string StripQuotes ( string s , string quoteChar )
		{
				return s.Replace ( quoteChar , String.Empty );
		} //eof method SripQotes ( string s , string quoteChar) 


        //finds and replace according to regex expr. in string
		public static string FindAndReplace (string s , string findRegex , string replaceRegex)
		{
				Utils.Debugger.WriteIf ( "My string to remove the replaceRegex - s is " + s );
				Utils.Debugger.WriteIf ( "My findRegex is " + findRegex );
				Utils.Debugger.WriteIf ( "My replaceRegex is " + replaceRegex );

		    Regex Remover = new Regex ( @"^" + findRegex + "$",
        RegexOptions.IgnoreCase | RegexOptions.Compiled );
				string toReturn = Remover.Replace ( s , replaceRegex );
			//	Utils.Debugger.WriteIf ( " I have to return from FindAndReplace " + toReturn );
				return toReturn;
		} //eof method FindAndReplace ()

        public static string FindAndReplaceCharInString(string s, string findRegex, string replaceRegex)
        {
            StringBuilder sb = new StringBuilder(s);
            sb.Replace("'" , "''");
            Utils.Debugger.WriteIf("The string after replacing from FindAndReplaceCharInString is "
                + sb.ToString()); 
            return sb.ToString();
        } //eof method FindAndReplace ()



        public static int NumberOfCharsInStr(ref string stringToSearch, char charToFind)
        {
            int count = 0;
            char[] chars = stringToSearch.ToCharArray();
            foreach (char c in chars)
            {
                if (c == charToFind)
                {
                    count++;
                }
            }
            return count;
        } //eof method 



        /// <summary>
        /// This method counts the number of lines in a string passed as the argument.
        /// It is benchmarked in this article, but what it does is make a new Regex and
        /// then get a MatchCollection on it, and then return that Count property.
        /// </summary>
        /// <param name="text">The string you want to count lines in.</param>
        /// <returns>The number of lines in the string.</returns>
        public static long CountLinesInStringSlow(string text)
        {
            Regex reg = new Regex("\n", RegexOptions.Multiline);
            MatchCollection mat = reg.Matches(text);
            return mat.Count + 1;
        }

        /// <summary>
        /// This method counts the number of lines in a string passed as the argument.
        /// It uses simple IndexOf and interation to count the newlines. I start
        /// count at 1 because there is always at least one line in the string.
        /// </summary>
        /// <param name="text">You want to count the lines in this.</param>
        /// <returns>The number of lines in the string.</returns>
        public static long CountLinesInString(string text)
        {
            long count = 1;
            int start = 0;
            while ((start = text.IndexOf('\n', start)) != -1)
            {
                count++;
                start++;
            }
            return count;
        }



		} // eof public class Str
} //eof namespace Utils.Str